package com.syqc.custom

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.MonthView


class IndexMonthView(context: Context) : MonthView(context) {


    private val mTextPaint = Paint()
    private var mRadio = 0f
    private var mPadding = 0

    init {
        mTextPaint.textSize = dipToPx(context, 8f).toFloat()
        mTextPaint.color = -0x1
        mTextPaint.isAntiAlias = true
        mTextPaint.isFakeBoldText = true
        mRadio = dipToPx(getContext(), 7f).toFloat()
        mPadding = dipToPx(getContext(), 4f)
    }

    /**
     * @param canvas    canvas
     * @param calendar  日历日历calendar
     * @param x         日历Card x起点坐标
     * @param y         日历Card y起点坐标
     * @param hasScheme hasScheme 非标记的日期
     * @return true 则绘制onDrawScheme，因为这里背景色不是是互斥的
     */
    override fun onDrawSelected(
        canvas: Canvas,
        calendar: Calendar?,
        x: Int,
        y: Int,
        hasScheme: Boolean
    ): Boolean {
        mSelectedPaint.style = Paint.Style.FILL
        canvas.drawRoundRect(
            (x + mPadding).toFloat(),
            (y + mPadding).toFloat(),
            (x + mItemWidth - mPadding).toFloat(),
            (y + mItemHeight - mPadding).toFloat(),8f,8f,
            mSelectedPaint
        )
        return true
    }

    override fun onDrawScheme(canvas: Canvas, calendar: Calendar, x: Int, y: Int) {

    }

    override fun onDrawText(
        canvas: Canvas,
        calendar: Calendar,
        x: Int,
        y: Int,
        hasScheme: Boolean,
        isSelected: Boolean
    ) {
        val cx = (x + mItemWidth / 2).toFloat()
        val cy = mTextBaseLine + y + mItemHeight / 10
        val top = y - mItemHeight / 6
        val cyt = mTextBaseLine + top
        if (isSelected) {
            canvas.drawText(calendar.day.toString(), cx, cyt, mSelectTextPaint)
            canvas.drawText(calendar.scheme?:"", cx, cy+6, mSelectedLunarTextPaint)
        } else if (hasScheme) {
            canvas.drawText(
                calendar.day.toString(), cx, cyt,
                if (calendar.isCurrentDay) mCurDayTextPaint else if (calendar.isCurrentMonth) mCurMonthTextPaint else mOtherMonthTextPaint
            )
            canvas.drawText(
                calendar.scheme?:"", cx, cy+6,
                if (calendar.isCurrentDay) mCurDayLunarTextPaint else mSchemeLunarTextPaint
            )
        } else {
            canvas.drawText(
                calendar.day.toString(), cx, cyt,
                if (calendar.isCurrentDay) mCurDayTextPaint else if (calendar.isCurrentMonth) mCurMonthTextPaint else mOtherMonthTextPaint
            )

            canvas.drawText(
                calendar.scheme?:"", cx, cy+6,
                if (calendar.isCurrentDay) mCurDayLunarTextPaint else if (calendar.isCurrentMonth) mCurMonthLunarTextPaint else mOtherMonthLunarTextPaint
            )
        }
    }

    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    private fun dipToPx(context: Context, dpValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dpValue * scale + 0.5f).toInt()
    }
}
